package com.navi.loaddata.loadService.impl.hisData;


import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mesEntity.MesProducthistory;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.repository.impl.MesRepositoryImpl;
import com.navi.loaddata.core.util.ProcessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(transactionManager = "secondaryPlatformTransactionManager")
public class LoadHisInfoService {

    @Autowired
    private MesRepositoryImpl mesdao;

    public Map<String, MesProducthistory> loadHisInInfo(EtlConfDModel etlConfD, String hisOutKey) {
        Map<String, MesProducthistory> mapHisIn = new HashMap<>();
        List<MesProducthistory> listHisIn = new ArrayList<>();

        StringBuffer hqlIn = new StringBuffer();
        hqlIn.append("FROM MesProducthistory WHERE EVENTNAME='TrackIn'")
                .append(" and (PRODUCTNAME,LINKKEY) in(")
                .append(hisOutKey)
                .append(")");
        hqlIn.append(ProcessUtil.getShopMesHql(etlConfD.getShop()))
                .append(ProcessUtil.getOpeMesHql(etlConfD.getOpe()))
                .append(ProcessUtil.getEqptMesHql(etlConfD.getEqpt()));
        try {
            listHisIn = mesdao.list(MesProducthistory.class, hqlIn.toString());
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_GET_MES_HIS_TRACKIN, e);
        }
        listHisIn.forEach(hisIn -> {
            mapHisIn.put(hisIn.getProductname() + "_" + hisIn.getLinkkey(), hisIn);
        });
        return mapHisIn;
    }

    public List<MesProducthistory> loadHisOutInfo(EtlConfDModel etlConfD) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        List<MesProducthistory> listHisOut = new ArrayList<>();
        String startTime = sdf.format(etlConfD.getRunStartTimestamp().getTime());
        String endTime = sdf.format(etlConfD.getRunEndTimestamp().getTime());
        StringBuffer hqlOut = new StringBuffer();

        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    hqlOut.append("FROM MesProducthistory WHERE  EVENTNAME='TrackOut' and OLDPROCESSOPERATIONNAME NOT IN ('A1100','C1100')")
                            .append(" and LENGTH(PRODUCTNAME)=10")
                            .append(" and UPDATETIME>=to_date('" + startTime + "','yyyymmddhh24miss')")
                            .append(" and UPDATETIME<to_date('" + endTime + "','yyyymmddhh24miss') ");
                    break;
                case "CF":
                    hqlOut.append("FROM MesProducthistory WHERE EVENTNAME='TrackOut' and OLDPROCESSOPERATIONNAME NOT IN ('A1100','C1100') ")
                            .append(" and LENGTH(PRODUCTNAME)=10")
                            .append(" and UPDATETIME>=to_date('" + startTime + "','yyyymmddhh24miss')")
                            .append(" and UPDATETIME<to_date('" + endTime + "','yyyymmddhh24miss') ");
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        hqlOut.append("FROM MesProducthistory WHERE  EVENTNAME='TrackOut' and OLDPROCESSOPERATIONNAME <'L4100'")
                                .append(" and LENGTH(PRODUCTNAME)=10")
                                .append(" and UPDATETIME>=to_date('" + startTime + "','yyyymmddhh24miss')")
                                .append(" and UPDATETIME<to_date('" + endTime + "','yyyymmddhh24miss') ");
                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        hqlOut.append("FROM MesProducthistory WHERE  EVENTNAME='TrackOut' and OLDPROCESSOPERATIONNAME >='L4100'")
                                .append(" and LENGTH(PRODUCTNAME)=12")
                                .append(" and UPDATETIME>=to_date('" + startTime + "','yyyymmddhh24miss')")
                                .append(" and UPDATETIME<to_date('" + endTime + "','yyyymmddhh24miss') ");
                    } else {
                        throw new ETLException2(ErrorCode.ERROR_CELL_TYPE, null);
                    }
                    break;
                default:
                    throw new ETLException2(ErrorCode.ERROR_SHOP, null);
            }
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_GET_MES_HIS_TRACKOUT, e);
        }

        hqlOut.append(ProcessUtil.getShopMesHql(etlConfD.getShop()))
                .append(ProcessUtil.getOpeMesHql(etlConfD.getOpe()))
                .append(ProcessUtil.getEqptMesHql(etlConfD.getEqpt()));
        return mesdao.list(MesProducthistory.class, hqlOut.toString());
    }

}
